﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NF_Tools
{
    public partial class BF_MenuControl : UserControl
    {
        public BF_MenuControl()
        {
            InitializeComponent();
        }


        # region 属性及内部方法
        private string[] listitem;
        [Browsable(true), Category("Pan"), Description("模拟菜单文字集合")]
        public string[] ListItem
        {
            get
            {
                return listitem;
            }
            set
            {
                listitem = value;
            }
        }



        private PictureBox[] defaultimages; //默认图片
        [Browsable(true), Category("Pan"), Description("模拟菜单图片集合")]
        public PictureBox[] DefaultImages
        {
            get { return defaultimages; }
            set
            {
                defaultimages = value;
            }
        }

        private bool _Checkit;
        public bool Checkit 
        {
            get { return _Checkit; }
            set 
            {
                if (value)
                    CreateMenu();
                _Checkit = value;
            }
        }


        private PictureBox[] hoverimages;   //鼠标经过时的图片

        [Browsable(true), Category("Pan"), Description("模拟菜单鼠标划过图片集合")]
        public PictureBox[] HoverImages
        {
            get { return hoverimages; }
            set
            {
                hoverimages = value;
            }
        }



        private Color _bdefaultcolor = Color.White;//背景颜色
        /// <summary>
        /// 默认菜单项背景色
        /// </summary>
        [Browsable(true), Category("Pan"), Description("菜单背景颜色")]
        public Color DefaultColor
        {
            get { return _bdefaultcolor; }
            set
            {
                _bdefaultcolor = value;
            }
        }

        private Color _bhovercolor = Color.FromArgb(240, 240, 240);//鼠标划过的颜色
        /// <summary>
        /// 鼠标经过菜单项背景颜色
        /// </summary>
        [Browsable(true), Category("Pan"), Description("菜单背景颜色")]
        public Color Hovercolor
        {
            get { return _bhovercolor; }
            set
            {
                _bhovercolor = value;
            }
        }

        private Color _fontcolor = Color.FromArgb(90, 90, 90);  //字体颜色
        /// <summary>
        /// 菜单项字体颜色
        /// </summary>
        [Browsable(true), Category("Pan"), Description("字体颜色")]
        public Color FontColor
        {
            get { return _fontcolor; }
            set
            {
                _fontcolor = value;
            }
        }


        //点击时字体的颜色
        private Color _fontclickcolor = Color.FromArgb(233, 81, 29);
        /// <summary>
        /// 鼠标点击菜单项字体颜色
        /// </summary>
        [Browsable(true), Category("Pan"), Description("鼠标经过文字时的颜色")]
        public Color FontClickColor
        {
            get { return _fontclickcolor; }
            set
            {
                _fontclickcolor = value;

            }
        }

        private Font _font = new Font(@"微软雅黑", 12);  //字体以及大小设置

        private float _rowheight = 55f;  //菜单高度
        /// <summary>
        /// 菜单项高度
        /// </summary>
        [Browsable(true), Category("Pan"), Description("鼠标经过文字时的颜色")]
        public float RowHeight
        {
            get { return _rowheight; }
            set
            {
                _rowheight = value;
            }
        }

        /// <summary>
        /// 图片距左的距离
        /// </summary>
        private int _picmarginleft = 50;
        [Browsable(true), Category("Pan"), Description("图片距左的距离")]
        public int PicMarginLeft
        {
            get { return _picmarginleft; }
            set
            {
                _picmarginleft = value;
            }
        }



        /// <summary>
        /// 菜单图片的高度和宽度
        /// </summary>
        private int _picheightandwidth = 20;
        [Browsable(true), Category("Pan"), Description("菜单图片的高度和宽度，正方形")]
        public int PicHeightAndWidth
        {
            get { return _picheightandwidth; }
            set
            {
                _picheightandwidth = value;
                //CreateMenu();
            }
        }

        /// <summary>
        ///解决选中控件后保存此控件的tag，并且在鼠标离开时，保持选中样式
        /// </summary>
        private int flaglabel = -1;
        /// <summary>
        /// 解决选中控件后，再次选中别的菜单，保存此控件，并且在鼠标离开时，保持选中样式
        /// </summary>
        private object send = new object();

        /// <summary>
        /// 当默认为第一个菜单项为选中的样式时，鼠标离开事件不触发,当为选中第一个菜单项时设置为true
        /// </summary>
        private bool NoSelectSatyle = false;

        /// <summary>
        /// 当点击的按钮不为默认的按钮时，改变第一个按钮样式为默认按钮
        /// </summary>
        private Label firstlbl = new Label();
        #endregion

        #region -生成菜单按钮方法-
        private void CreateMenu()
        {
            if (ListItem == null) return;
            if (DefaultImages == null) return;
            if (ListItem.Count() != DefaultImages.Count() || DefaultImages.Count() != HoverImages.Count()) return;


            TableLayoutPanel tpanel = new TableLayoutPanel();
            tpanel.Dock = DockStyle.Fill;
            tpanel.ColumnCount = 1;
            tpanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
            tpanel.RowCount = ListItem.Count() + 1;
            for (int i = 0; i < ListItem.Count(); i++)
            {
                //每次创建一行，并将行的宽度设置为100%
                tpanel.RowStyles.Add(new RowStyle(SizeType.Absolute, RowHeight));  //设置行高
                FlowLayoutPanel p = new FlowLayoutPanel();
                p.Dock = DockStyle.Top;
                p.BackColor = DefaultColor; //设置默认背景颜色
                p.Margin = new Padding(0);
                p.Height = int.Parse(RowHeight.ToString());
                p.Tag = i;
                p.MouseEnter += Lbl_MouseEnter;
                p.MouseLeave += Lbl_MouseLeave;
                p.MouseHover += Lbl_MouseHover;
                p.Click += P_Click;

                PictureBox pic = new PictureBox();
                pic.Height = PicHeightAndWidth;
                pic.Width = PicHeightAndWidth;
                pic.Image = DefaultImages[i].Image;
                pic.Tag = i;
                pic.SizeMode = PictureBoxSizeMode.Zoom;
                pic.Anchor = AnchorStyles.None;
                pic.Margin = new Padding(PicMarginLeft, (p.Height - pic.Height) / 2, 0, 0);
                pic.Click += P_Click;

                Label lbl = new Label();
                lbl.Font = _font;
                lbl.ForeColor = FontColor;  //设置字体颜色
                lbl.Text = ListItem[i];
                lbl.Margin = new Padding(5, (p.Height - lbl.Height) / 2, 0, 0);
                lbl.TextAlign = ContentAlignment.MiddleLeft;
                lbl.AutoSize = true;
                lbl.Tag = i;
                lbl.MouseHover += Lbl_MouseHover;
                lbl.MouseEnter += Lbl_MouseEnter;
                lbl.MouseLeave += Lbl_MouseLeave;
                lbl.Click += P_Click;

                if (i == 0)
                {
                    p.BackColor = Hovercolor;
                    pic.Image = HoverImages[i].Image;
                    lbl.ForeColor = FontClickColor;
                    NoSelectSatyle = true;
                    firstlbl = lbl;

                }
                else
                {
                    NoSelectSatyle = false;
                }
                p.Controls.AddRange(new Control[] { pic, lbl });
                //在每次创建的行当中添加图片和lal
                tpanel.Controls.Add(p, 0, i);
            }
            this.Controls.Add(tpanel);

        }

        #endregion

        #region --抛出的方法--
        public delegate void UserClickDelegate(object sender, EventArgs e, int tag);
        [Browsable(true), Description("菜单点击事件"), Category("Pan")]
        public event UserClickDelegate UserClickEvent;

        private void P_Click(object sender, EventArgs e)
        {
            int tag = SetClickStyles(sender);
            if (tag != -1)
            {
                if (flaglabel != tag && flaglabel != -1)
                {
                    SetClickAfterStyle(send);
                }
                flaglabel = tag;
                send = sender;

                if (UserClickEvent != null)
                    UserClickEvent(sender, e, tag);
            }
        }
        #endregion

        #region -选中之后改变菜单样式-

        private int SetClickStyles(object sender)
        {
            int tag = -1;
            if (sender is Label)
            {
                Label lbl = sender as Label;
                tag = int.Parse(lbl.Parent.Tag.ToString());
                lbl.ForeColor = FontClickColor;
                ((PictureBox)lbl.Parent.Controls[0]).Image = HoverImages[tag].Image;
                lbl.Parent.BackColor = Hovercolor;
            }
            if (sender is FlowLayoutPanel)
            {
                FlowLayoutPanel flow = sender as FlowLayoutPanel;
                tag = int.Parse(flow.Tag.ToString());
                flow.BackColor = Hovercolor;
                ((PictureBox)flow.Controls[0]).Image = HoverImages[tag].Image;
                ((Label)flow.Controls[1]).ForeColor = FontClickColor;
            }
            if (sender is PictureBox)
            {
                PictureBox pic = sender as PictureBox;
                tag = int.Parse(pic.Parent.Tag.ToString());
                pic.Image = HoverImages[tag].Image;
                ((Label)pic.Parent.Controls[1]).ForeColor = FontClickColor; ;
                pic.Parent.BackColor = Hovercolor;
            }
            if (tag != -1)
            {
                ChageFirstStyle(tag);
            }
            return tag;
        }

        /// <summary>
        /// 设置按钮点击后样式
        /// </summary>
        /// <param name="sender"></param>
        private void SetClickAfterStyle(object sender)
        {
            int tag = -1;
            if (sender is Label)
            {
                Label lbl = sender as Label;
                tag = int.Parse(lbl.Parent.Tag.ToString());
                lbl.ForeColor = FontColor;
                ((PictureBox) lbl.Parent.Controls[0]).Image = DefaultImages[tag].Image;
                lbl.Parent.BackColor = DefaultColor;
            }
            if (sender is FlowLayoutPanel)
            {
                FlowLayoutPanel flow = sender as FlowLayoutPanel;
                tag = int.Parse(flow.Tag.ToString());
                flow.BackColor = DefaultColor;
                ((PictureBox) flow.Controls[0]).Image = DefaultImages[tag].Image;
                ((Label) flow.Controls[1]).ForeColor = FontColor;
            }
            if (sender is PictureBox)
            {
                PictureBox pic = sender as PictureBox;
                tag = int.Parse(pic.Parent.Tag.ToString());
                pic.Image = DefaultImages[tag].Image;
                ((Label) pic.Parent.Controls[1]).ForeColor = FontColor;
                pic.Parent.BackColor = Hovercolor;
            }

        }

        /// <summary>
        /// 当选中的菜单项不为默认的菜单项时改变第一个菜单项样式
        /// </summary>
        private void ChageFirstStyle(int tag)
        {
            if(tag!=0)
            {
                firstlbl.ForeColor = FontColor;

                firstlbl.Parent.BackColor = DefaultColor;

                ((PictureBox)firstlbl.Parent.Controls[0]).Image = DefaultImages[0].Image;
            }
        }
        #endregion

        #region --lable MouseLeave MouseEnter MouseHover事件--

        /// <summary>
        /// 鼠标离开label,获取他的父节点与兄弟节点共同设置样式
        /// </summary>
        private void Lbl_MouseLeave(object sender, EventArgs e)
        {
            //当点击的为label
            if (sender is Label)
            {
                Label lbl = sender as Label;
                int tag = int.Parse(lbl.Tag.ToString());
                if (flaglabel != tag && tag!=0)
                {
                    //设置菜单背景色
                    lbl.Parent.BackColor = DefaultColor;
                    //设置文字颜色
                    lbl.ForeColor = FontColor;
                    //设置图片
                    ((PictureBox) lbl.Parent.Controls[0]).Image = DefaultImages[tag].Image;
                }

            }

            if (sender is FlowLayoutPanel)
            {
                FlowLayoutPanel lbl = sender as FlowLayoutPanel;
                int tag = int.Parse(lbl.Tag.ToString());
                if (flaglabel != tag && tag != 0)
                {
                    //设置菜单背景色
                    lbl.BackColor = DefaultColor;
                    //设置图片
                    ((PictureBox) lbl.Controls[0]).Image = DefaultImages[tag].Image;
                    //设置文字颜色
                    ((Label) lbl.Controls[1]).ForeColor = FontColor;
                }
            }

        }

        /// <summary>
        /// 鼠标进入label，获取他的父节点与兄弟节点共同设置样式
        /// </summary>
        private void Lbl_MouseEnter(object sender, EventArgs e)
        {
            if (sender is Label)
            {
                Label lbl = sender as Label;
                lbl.Parent.BackColor = Hovercolor;
            }
            if (sender is FlowLayoutPanel)
            {
                FlowLayoutPanel lbl = sender as FlowLayoutPanel;
                lbl.BackColor = Hovercolor;
            }
        }

        /// <summary>
        /// 鼠标经过label,获取他的父节点与兄弟节点共同设置样式
        /// </summary>
        private void Lbl_MouseHover(object sender, EventArgs e)
        {
            if (sender is Label)
            {
                Label lbl = sender as Label;
                lbl.Parent.BackColor = Hovercolor;
            }
            if (sender is FlowLayoutPanel)
            {
                FlowLayoutPanel lbl = sender as FlowLayoutPanel;
                lbl.BackColor = Hovercolor;
            }
        }

        #endregion
    }
}
